package com.ysy.persimmons.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.ysy.persimmons.common.enums.ComResponseCodeEnum;
import com.ysy.persimmons.common.exception.BaseException;
import com.ysy.persimmons.common.exception.BaseExceptionInterface;
import com.ysy.persimmons.common.exception.ServiceException;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 断言校验工具类
 * @author yangSy git地址：git地址：url:<a href="https://gitee.com/ysyGit01/persimmons"/>
 * @date 2025/3/1
 */

public class AssertUtil {


    //如果不是true，则抛异常
    public static void isTrue(boolean expression, String msg) {
        if (!expression) {
            throwException(msg);
        }
    }
    public static void isTrue(boolean expression, BaseExceptionInterface errorEnum, Object... args) {
        if (!expression) {
            throwException(errorEnum, args);
        }
    }

    //如果是true，则抛异常
    public static void isFalse(boolean expression, String msg) {
        if (expression) {
            throwException(msg);
        }
    }

    //如果是true，则抛异常
    public static void isFalse(boolean expression, BaseExceptionInterface errorEnum, Object... args) {
        if (expression) {
            throwException(errorEnum, args);
        }
    }

    //如果不是非空对象，则抛异常
    public static void isNotEmpty(Object obj, String msg) {
        if (isEmpty(obj)) {
            throwException(msg);
        }
    }

    //如果不是非空对象，则抛异常
    public static void isNotEmpty(Object obj, BaseExceptionInterface errorEnum, Object... args) {
        if (isEmpty(obj)) {
            throwException(errorEnum, args);
        }
    }

    /**
     * 如果不是非空对象，则抛异常
     */
    public static void isEmpty(Object obj, String msg) {
        if (!isEmpty(obj)) {
            throwException(msg);
        }
    }

    public static void equal(Object o1, Object o2, String msg) {
        if (!ObjectUtil.equal(o1, o2)) {
            throwException(msg);
        }
    }

    public static void notEqual(Object o1, Object o2, String msg) {
        if (ObjectUtil.equal(o1, o2)) {
            throwException(msg);
        }
    }

    private static boolean isEmpty(Object obj) {
        return ObjectUtil.isEmpty(obj);
    }

    private static void throwException(String msg) {
        throwException(null, msg);
    }

    private static void throwException(BaseExceptionInterface errorEnum, Object... arg) {
       if (errorEnum==null){
           String message = Arrays.stream(arg)
                   .map(Object::toString)
                   .collect(Collectors.joining(", "));
           throw new ServiceException(new BaseException("500", message));
       }
        throw new ServiceException(errorEnum);
    }


}
